package com.jida.hadoop.mr.UV4;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import com.jida.hadoop.mr.tools.TProperties;

public class UVCountReducer extends Reducer<Text,Text,NullWritable,Text>{
	private Text ovalue;
	@Override
	protected void reduce(Text key, Iterable<Text> values,
			Reducer<Text, Text, NullWritable, Text>.Context context)
			throws IOException, InterruptedException {
		String ps1;
		String psal="";
		String ps2;
		int uv=0;
		int pv=0;
		Iterator<Text> it=values.iterator();
		while(it.hasNext()){
			Text tmp=it.next();
			ps1 = tmp.toString().split(TProperties.getValue("fileoutsplit"))[0];
			ps2 = tmp.toString().split(TProperties.getValue("fileoutsplit"))[1];
			if(!psal.contains(ps1)) {
				uv++;
				psal=psal+ps1;
			}
			pv=pv+Integer.parseInt(ps2);
		}
		StringBuffer sb = new StringBuffer();
		sb.append(key).append(TProperties.getValue("outfilesplit"))
		.append(uv).append(TProperties.getValue("outfilesplit")).append(pv);
		ovalue = new Text(sb.toString());
		context.write(NullWritable.get(), ovalue);
	}
}
